{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3cdddf0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1\n",
      "2 4\n",
      "3 9\n"
     ]
    }
   ],
   "source": [
    "for x,y in [(1,1),(2,4),(3,9)]:\n",
    "    print(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4b371a41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试成功!\n"
     ]
    }
   ],
   "source": [
    "def findMinAndMax(L):\n",
    "    if L == []:\n",
    "        return (None,None)\n",
    "    min = L[0]\n",
    "    max = L[0]\n",
    "    for x in L:\n",
    "        if min > x:\n",
    "            min = x\n",
    "        elif max <x:\n",
    "            max = x\n",
    "    return (min, max)\n",
    "\n",
    "# 测试\n",
    "if findMinAndMax([]) != (None, None):\n",
    "    print('测试失败!')\n",
    "elif findMinAndMax([7]) != (7, 7):\n",
    "    print('测试失败!')\n",
    "elif findMinAndMax([7, 1]) != (1, 7):\n",
    "    print('测试失败!')\n",
    "elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):\n",
    "    print('测试失败!')\n",
    "else:\n",
    "    print('测试成功!')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "410aabb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试成功5!\n"
     ]
    }
   ],
   "source": [
    "def findMinAndMax(L):\n",
    "    if L == []:\n",
    "        return (None,None)\n",
    "    min = L[0]\n",
    "    max = L[0]\n",
    "    x = 0\n",
    "    for x in L:\n",
    "        if min > x:\n",
    "            min = x\n",
    "        if max < x:\n",
    "            max = x\n",
    "    return (min,max)\n",
    "\n",
    "\n",
    "# 测试\n",
    "if findMinAndMax([]) != (None, None):\n",
    "    print('测试失败1!')\n",
    "elif findMinAndMax([7]) != (7, 7):\n",
    "    print('测试失败2!')\n",
    "elif findMinAndMax([7, 1]) != (1, 7):\n",
    "    print('测试失败3!')\n",
    "elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):\n",
    "    print('测试失败4!')\n",
    "else:\n",
    "    print('测试成功5!')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8840ce83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world', 'apple']\n",
      "测试通过!\n"
     ]
    }
   ],
   "source": [
    "L1 = ['Hello', 'World', 18,'Apple',None]\n",
    "L2 = [x.lower() for x in L1 if isinstance(x,str)]\n",
    "\n",
    "# 测试:\n",
    "print(L2)\n",
    "if L2 == ['hello', 'world', 'apple']:\n",
    "    print('测试通过!')\n",
    "else:\n",
    "    print('测试失败!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3f633281",
   "metadata": {},
   "outputs": [],
   "source": [
    "L2 = [x.lower() for x in L1 if isinstance(x, str)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "001a1a3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<generator object fib at 0x000002A6A09EBBC0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fib(max):\n",
    "    n, a, b = 0, 0, 1\n",
    "    while n < max:\n",
    "        yield b\n",
    "        a, b = b, a + b\n",
    "        n = n + 1\n",
    "    return 'done'\n",
    "\n",
    "f = fib(6)\n",
    "f\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f967954c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n",
      "[1, 1]\n",
      "[1, 2, 1]\n",
      "[1, 3, 3, 1]\n",
      "[1, 4, 6, 4, 1]\n",
      "[1, 5, 10, 10, 5, 1]\n",
      "[1, 6, 15, 20, 15, 6, 1]\n",
      "[1, 7, 21, 35, 35, 21, 7, 1]\n",
      "[1, 8, 28, 56, 70, 56, 28, 8, 1]\n",
      "[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]\n",
      "测试通过!\n"
     ]
    }
   ],
   "source": [
    "def triangles():\n",
    "    row = [1]\n",
    "    while True:\n",
    "        yield row\n",
    "        row = [1] + [row[i] + row[i+1] for i in range(len(row)-1)] + [1]\n",
    "\n",
    "# 期待输出:\n",
    "# [1]\n",
    "# [1, 1]\n",
    "# [1, 2, 1]\n",
    "# [1, 3, 3, 1]\n",
    "# [1, 4, 6, 4, 1]\n",
    "# [1, 5, 10, 10, 5, 1]\n",
    "# [1, 6, 15, 20, 15, 6, 1]\n",
    "# [1, 7, 21, 35, 35, 21, 7, 1]\n",
    "# [1, 8, 28, 56, 70, 56, 28, 8, 1]\n",
    "# [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]\n",
    "n = 0\n",
    "results = []\n",
    "for t in triangles():\n",
    "    results.append(t)\n",
    "    n = n + 1\n",
    "    if n == 10:\n",
    "        break\n",
    "\n",
    "for t in results:\n",
    "    print(t)\n",
    "\n",
    "if results == [\n",
    "    [1],\n",
    "    [1, 1],\n",
    "    [1, 2, 1],\n",
    "    [1, 3, 3, 1],\n",
    "    [1, 4, 6, 4, 1],\n",
    "    [1, 5, 10, 10, 5, 1],\n",
    "    [1, 6, 15, 20, 15, 6, 1],\n",
    "    [1, 7, 21, 35, 35, 21, 7, 1],\n",
    "    [1, 8, 28, 56, 70, 56, 28, 8, 1],\n",
    "    [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]\n",
    "]:\n",
    "    print('测试通过!')\n",
    "else:\n",
    "    print('测试失败!')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f4cc05ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n",
      "[1, 1]\n",
      "[1, 2, 1]\n",
      "[1, 3, 3, 1]\n",
      "[1, 4, 6, 4, 1]\n",
      "[1, 5, 10, 10, 5, 1]\n",
      "[1, 6, 15, 20, 15, 6, 1]\n",
      "[1, 7, 21, 35, 35, 21, 7, 1]\n",
      "[1, 8, 28, 56, 70, 56, 28, 8, 1]\n",
      "[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]\n",
      "测试通过!\n"
     ]
    }
   ],
   "source": [
    "def triangles():\n",
    "    line = 0\n",
    "    row = []\n",
    "    while True:\n",
    "        if line == 0:\n",
    "            row = [1]\n",
    "            yield row\n",
    "        elif line == 1:\n",
    "            row = [1,1]\n",
    "            yield row\n",
    "        else :\n",
    "            row = [1] + [row[i] + row[i+1] for i in range(line-1)] + [1]\n",
    "            yield row\n",
    "        line += 1\n",
    "\n",
    "# 期待输出:\n",
    "# [1]\n",
    "# [1, 1]\n",
    "# [1, 2, 1]\n",
    "# [1, 3, 3, 1]\n",
    "# [1, 4, 6, 4, 1]\n",
    "# [1, 5, 10, 10, 5, 1]\n",
    "# [1, 6, 15, 20, 15, 6, 1]\n",
    "# [1, 7, 21, 35, 35, 21, 7, 1]\n",
    "# [1, 8, 28, 56, 70, 56, 28, 8, 1]\n",
    "# [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]\n",
    "n = 0\n",
    "results = []\n",
    "for t in triangles():\n",
    "    results.append(t)\n",
    "    n = n + 1\n",
    "    if n == 10:\n",
    "        break\n",
    "\n",
    "for t in results:\n",
    "    print(t)\n",
    "\n",
    "if results == [\n",
    "    [1],\n",
    "    [1, 1],\n",
    "    [1, 2, 1],\n",
    "    [1, 3, 3, 1],\n",
    "    [1, 4, 6, 4, 1],\n",
    "    [1, 5, 10, 10, 5, 1],\n",
    "    [1, 6, 15, 20, 15, 6, 1],\n",
    "    [1, 7, 21, 35, 35, 21, 7, 1],\n",
    "    [1, 8, 28, 56, 70, 56, 28, 8, 1],\n",
    "    [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]\n",
    "]:\n",
    "    print('测试通过!')\n",
    "else:\n",
    "    print('测试失败!')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9d33b2d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "16\n",
      "36\n",
      "64\n"
     ]
    }
   ],
   "source": [
    "# 创建生成器表达式（本质是迭代器）\n",
    "even_squares = (x**2 for x in range(1, 10) if x % 2 == 0)\n",
    "\n",
    "# 手动迭代\n",
    "print(next(even_squares))  # 2² = 4\n",
    "print(next(even_squares))  # 4² = 16\n",
    "print(next(even_squares))  # 6² = 36\n",
    "print(next(even_squares))  # 8² = 64\n",
    "\n",
    "# # 第五次调用 → 触发 StopIteration\n",
    "# print(next(even_squares))  # 报错: StopIteration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e37a6026",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "16\n",
      "36\n",
      "64\n"
     ]
    }
   ],
   "source": [
    "for num in (x**2 for x in range(1, 10) if x % 2 == 0):\n",
    "    print(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f797f230",
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import reduce\n",
    "\n",
    "DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}\n",
    "\n",
    "def str2int(s):\n",
    "    def fn(x,y):\n",
    "        return x * 10 + y\n",
    "    def char2num(s):\n",
    "        return DIGITS[s]\n",
    "    return reduce(fn,map(char2num,s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c58357b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import reduce\n",
    "\n",
    "DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}\n",
    "\n",
    "def char2num(s):\n",
    "    return DIGITS[s]\n",
    "\n",
    "def str2int(s):\n",
    "    return reduce(lambda x,y:x*10+y,map(char2num,s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7ce31d97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Adam', 'Lisa', 'Bart']\n"
     ]
    }
   ],
   "source": [
    "def normalize(name):\n",
    "    return name[0].upper() + name[1:].lower()\n",
    "\n",
    "# 测试:\n",
    "L1 = ['adam', 'LISA', 'barT']\n",
    "L2 = list(map(normalize, L1))\n",
    "print(L2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ab32c87c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 * 5 * 7 * 9 = 945\n",
      "测试成功!\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "\n",
    "def prod(L):\n",
    "    return reduce(lambda x,y:x*y,L)\n",
    "\n",
    "print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))\n",
    "if prod([3, 5, 7, 9]) == 945:\n",
    "    print('测试成功!')\n",
    "else:\n",
    "    print('测试失败!')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8a2f6008",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.456\n",
      "str2float('123.456') = 123.456\n",
      "0.456\n",
      "测试成功!\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "\n",
    "def str2float(s):\n",
    "    i,f = s.split('.')\n",
    "    i_number = reduce(lambda x,y:x*10+y,map(int,i))\n",
    "    f_number = reduce(lambda x,y:x*10+y,map(int,f)) / 10 **len(f)\n",
    "    print(f_number)\n",
    "    return i_number + f_number\n",
    "\n",
    "print('str2float(\\'123.456\\') =', str2float('123.456'))\n",
    "if abs(str2float('123.456') - 123.456) < 0.00001:\n",
    "    print('测试成功!')\n",
    "else:\n",
    "    print('测试失败!')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b8599e39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "5\n",
      "7\n",
      "11\n",
      "13\n",
      "17\n",
      "19\n",
      "23\n",
      "29\n",
      "31\n",
      "37\n",
      "41\n",
      "43\n",
      "47\n",
      "53\n",
      "59\n",
      "61\n",
      "67\n",
      "71\n",
      "73\n",
      "79\n",
      "83\n",
      "89\n",
      "97\n"
     ]
    }
   ],
   "source": [
    "def _odd_iter():\n",
    "    n = 1\n",
    "    while True:\n",
    "        n = n + 2\n",
    "        yield n\n",
    "\n",
    "def _not_divisible(n):\n",
    "    return lambda x: x % n > 0\n",
    "\n",
    "def primes():\n",
    "    yield 2\n",
    "    it = _odd_iter()\n",
    "    while True:\n",
    "        n = next(it)\n",
    "        yield n\n",
    "        it = filter(_not_divisible(n),it)\n",
    "\n",
    "for n in primes():\n",
    "    if n < 100:\n",
    "        print(n)\n",
    "    else:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d7686ab3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1~1000: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484, 494, 505, 515, 525, 535, 545, 555, 565, 575, 585, 595, 606, 616, 626, 636, 646, 656, 666, 676, 686, 696, 707, 717, 727, 737, 747, 757, 767, 777, 787, 797, 808, 818, 828, 838, 848, 858, 868, 878, 888, 898, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999]\n",
      "测试成功!\n"
     ]
    }
   ],
   "source": [
    "def is_palindrome(n):\n",
    "    return str(n) == str(n)[::-1]\n",
    "\n",
    "# 测试:\n",
    "output = filter(is_palindrome, range(1, 1000))\n",
    "print('1~1000:', list(output))\n",
    "if list(filter(is_palindrome, range(1, 200))) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]:\n",
    "    print('测试成功!')\n",
    "else:\n",
    "    print('测试失败!')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f9a5a9b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Credit', 'Zoo', 'about', 'bob']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(['bob', 'about', 'Zoo', 'Credit'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "09221b59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['about', 'bob', 'Credit', 'Zoo']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(['bob', 'about', 'Zoo', 'Credit'],key=str.lower)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b2fb6ebb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Zoo', 'Credit', 'bob', 'about']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(['bob', 'about', 'Zoo', 'Credit'],key=str.lower,reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d6132800",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('Bart', 66), ('Bob', 75), ('Lisa', 88), ('Adam', 92)]\n",
      "[('Adam', 92), ('Bart', 66), ('Bob', 75), ('Lisa', 88)]\n"
     ]
    }
   ],
   "source": [
    "L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]\n",
    "\n",
    "def by_score(t):\n",
    "    return t[1]\n",
    "\n",
    "L1 = sorted(L, key=by_score)\n",
    "print(L1)\n",
    "\n",
    "def by_name(t):\n",
    "    return t[0]\n",
    "\n",
    "L2 = sorted(L, key=by_name)\n",
    "print(L2)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d362dea8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
